<?php

namespace app\index\controller;
use app\admin\service\PhoneService;
use app\common\model;
use app\common\service\DeviceService;
use app\common\service\MacService;
use app\common\service\PushService;
class Notify extends Base {
    
    public $phoneModel;
    public function __construct () {
        parent::__construct();
        $this->phoneModel = new model\Phone();
    }
    public function MQNCJ9Wn () {
        //addlog();
    }
    public function index () {
        //addlog();
        $input = input();
        

        $message = $input['message'];
        $list = explode(PHP_EOL,$message);
        foreach($list as $v) {
            if(!$v) {
                continue;
            }
            $arr = explode(',',$v);
            if(!$arr[0] || !$arr[1] || !$arr[2]) {
                continue;
            }
            $data = [
                'device_id' => $input['device_id'],
                'create_time' => getDateTime(),
                'phone_mac' => $arr[0],
                'router_mac' => $arr[1],
                'ssid' => $arr[2],
                'is_mobile' => MacService::S()->isMobile($arr[0]),
                'brand' => MacService::S()->getBrand($arr[0])
            ];
            $old = $this->phoneModel->getOne([
                'phone_mac' => $arr[0],
                'router_mac' => $arr[1],
                'ssid' => $arr[2]
            ]);
            if($old) {
                $this->phoneModel->where('id',$old['id'])->update([
                    'update_time' => getDateTime()
                ]);
                continue;
            }
            $data['id'] = $this->phoneModel->insertGetId($data);
            PushService::S()->push($data);
        }
        echo 1;
    }


    //测试发送
    public function testsend(){
        if($_POST){
            $phone=DeviceService::S()->queryTestDeviceInfo();
            $phone_mac=input('phone_mac');
            if(empty($phone_mac)){
                echo json_encode(202,'phone_mac不能为空');
            }
            $ssid=input('ssid');
            if(empty($ssid)){
                $this->ajax(202,'ssid不能为空');
            }
            //查询出该mac地址是否存在phone_id
            $re=PhoneService::S()->findPhoneIdByPhoneMac($phone_mac);
            if($re){
                $phone['id']=$re['id'];
            }else{
                $add=[
                    'phone_mac'=>$phone_mac,
                    'device_id'=>$phone['device_id'],
                    'router_mac'=>'自定义',
                    'ssid'=>$ssid,
                    'update_time'=>date('Y-m-d H:i:s'),
                    'create_time'=>date('Y-m-d H:i:s'),
                    'is_mobile'=>1
                ];
                $phone['id']=PhoneService::S()->addPhoneList($add);
            }
            $phone['phone_mac']=$phone_mac;
            $phone['ssid']=$ssid;
            $phone['is_mobile']=1;
            $re=PushService::S()->push($phone,2);
            if($re){
                $this->ajax(200,'success');
            }else{
                $this->ajax(202,'error');
            }
        }

    }

  
}
